package com.neusoft.SPNursingHome.service;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.neusoft.SPNursingHome.dao.BedDao;
import com.neusoft.SPNursingHome.dao.BedRecordDao;
import com.neusoft.SPNursingHome.dao.CheckOutRecordDao;
import com.neusoft.SPNursingHome.dao.ClientDao;
import com.neusoft.SPNursingHome.po.*;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Map;

@Service
public class CheckOutRecordServiceImpl implements CheckOutRecordService {
    @Resource
    private CheckOutRecordDao checkOutRecordDao;
    @Resource
    private ClientDao clientDao;
    @Resource
    private BedDao bedDao;
    @Resource
    private BedRecordDao bedRecordDao;

    @Override
    public ResponseBean<IPage<CheckOutRecord>> listRequests(long pageNum, long pageSize, Map<String, Object> query) {
        Page<CheckOutRecord> page = new Page<>(pageNum, pageSize);
        IPage<CheckOutRecord> resultPage = checkOutRecordDao.findCheckOutRecordsPage(page, query);
        return new ResponseBean<>(resultPage);
    }

    @Override
    public ResponseBean<Void> applyForCheckOut(CheckOutRecord request) {
        // 从传入的 CheckOutRecord 对象中设置默认值
        request.setCheckOutReviewStatus("已提交"); // 已提交 (默认状态)
        checkOutRecordDao.insert(request);
        return new ResponseBean<>(200, "退住申请提交成功");
    }

    @Override
    @Transactional
    public ResponseBean<Void> auditRequest(CheckOutRecord auditInfo) {
        Integer checkOutId = auditInfo.getCheckOutId(); // 获取要审核的退房记录ID
        Integer adminId = auditInfo.getAdId(); // 获取审核人ID
        // 1. 获取数据库中完整的、原始的申请记录
        CheckOutRecord recordToUpdate = checkOutRecordDao.selectById(checkOutId);
        if (recordToUpdate == null) throw new RuntimeException("申请记录不存在");

        // 2. 使用 auditInfo 中的数据更新记录
        recordToUpdate.setCheckOutReviewStatus(auditInfo.getCheckOutReviewStatus());
        recordToUpdate.setCheckOutDescription(auditInfo.getCheckOutDescription());
        recordToUpdate.setAdId(adminId); // 审核人是当前管理员
        recordToUpdate.setCheckOutReviewTime(new Date());
        checkOutRecordDao.updateById(recordToUpdate);

        // 3. 如果审核通过且为特定类型，则执行联动操作
        boolean isApproved = "通过".equals(auditInfo.getCheckOutReviewStatus()); // 通过
        boolean needsAction = "正常退住".equals(recordToUpdate.getCheckOutType()) || "死亡退住".equals(recordToUpdate.getCheckOutType());

        if (isApproved && needsAction) {
            Client client = clientDao.selectById(recordToUpdate.getClientId());
            if (client != null && client.getBedId() != null) {
                // a. 修改床位状态为空闲
                Bed bedToUpdate = new Bed();
                bedToUpdate.setBedId(client.getBedId());
                bedToUpdate.setBedStatus("空闲");
                bedDao.updateById(bedToUpdate);

                // b. 设置当前床位记录的结束时间
                UpdateWrapper<BedRecord> brWrapper = new UpdateWrapper<>();
                brWrapper.eq("client_id", recordToUpdate.getClientId())
                        .eq("bed_record_status", 1)
                        .set("bed_end_time", recordToUpdate.getCheckOutTime())
                        .set("bed_record_status", 0);
                bedRecordDao.update(null, brWrapper);

                // 客户逻辑删除
                client.setClientStatus(0); // 设置客户状态为已删除
                clientDao.updateById(client);
            }
        }
        return new ResponseBean<>(200, "审核操作完成");
    }
}
